package com.smartian.v2;


import com.smartian.v2.impl.DefaultTextParser;
import com.smartian.v2.impl.EveryDayTextParser;
import com.smartian.v2.impl.EveryWorkDayTextParser;
import com.smartian.v2.impl.MonthlyTextParser;
import com.smartian.v2.impl.WeeklyTextParser;
import com.smartian.v2.impl.YearlyTextParser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by tianweitong on 2020/3/13.
 */

public abstract class TextParserFactory {

    public static AbstractTextParser createParser(String sourceText){

        RepeatInfo repeatInfo = TextParserFactory.getRepeatInfo(sourceText);
        AbstractTextParser textParser = null;
        switch (repeatInfo.repeatType){
            case TextPattern.REPEAT_EVERY_DAY:
                textParser = createEveryDayTextParser(sourceText,repeatInfo.patternText,repeatInfo.index);
                break;
            case TextPattern.REPEAT_EVERY_WORK_DAY:
                textParser = createEveryWorkDayTextParser(sourceText,repeatInfo.patternText,repeatInfo.index);
                break;
            case TextPattern.REPEAT_WEELY:
                textParser = createWeeklyTextParser(sourceText,repeatInfo.patternText,repeatInfo.index);
                break;
            case TextPattern.REPEAT_MONTHLY:
                textParser = createMonthlyTextParser(sourceText,repeatInfo.patternText,repeatInfo.index);
                break;
            case TextPattern.REPEAT_YEARLY:
                textParser = createYearlyTextParser(sourceText,repeatInfo.patternText,repeatInfo.index);
                break;
            case TextPattern.REPEAT_NONE:
            default:
                textParser = createDefaultTextParser(sourceText,repeatInfo.patternText,repeatInfo.index);
                break;
        }

        return textParser;

    }

    private static AbstractTextParser createDefaultTextParser(String sourceText, String patternText, int index) {
        return  new DefaultTextParser(sourceText,patternText,index);
    }

    private static AbstractTextParser createMonthlyTextParser(String sourceText, String patternText, int index) {
        return  new MonthlyTextParser(sourceText,patternText,index);
    }

    private static AbstractTextParser createYearlyTextParser(String sourceText, String patternText, int index) {
        return  new YearlyTextParser(sourceText,patternText,index);
    }

    private static AbstractTextParser createWeeklyTextParser(String sourceText, String patternText, int index) {
        return new WeeklyTextParser(sourceText,patternText,index);
    }

    private static AbstractTextParser createEveryDayTextParser(String sourceText, String patternText, int index) {
        return new EveryDayTextParser(sourceText,patternText,index);
    }

    private static AbstractTextParser createEveryWorkDayTextParser(String sourceText, String patternText, int index) {
        return new EveryWorkDayTextParser(sourceText,patternText,index);
    }

    public static RepeatInfo getRepeatInfo(String sourceText){

        RepeatInfo info = new RepeatInfo(sourceText);

        checkRepeatPatternInfo(info,TextPattern.REG_EXP_YEAR,TextPattern.REPEAT_YEARLY);
        checkRepeatPatternInfo(info, TextPattern.REG_EXP_MONTH_IN_YEAR, TextPattern.REPEAT_MONTHLY);

        checkRepeatPatternInfo(info,TextPattern.REG_EXP_MONTH,TextPattern.REPEAT_MONTHLY);
        checkRepeatPatternInfo(info, TextPattern.REG_EXP_DAY_IN_MONTH, TextPattern.REPEAT_EVERY_DAY);

        checkRepeatPatternInfo(info,TextPattern.REG_EXP_WEEK,TextPattern.REPEAT_WEELY);

        checkRepeatPatternInfo(info,TextPattern.REG_EXP_DAY,TextPattern.REPEAT_EVERY_DAY);

        checkRepeatPatternInfo(info,TextPattern.REG_EXP_WORK_DAY,TextPattern.REPEAT_EVERY_WORK_DAY);

        return info;

    }

    private static void checkRepeatPatternInfo(RepeatInfo info, Pattern pattern,int expectRepeatType) {

        Matcher matcher = pattern.matcher(info.text);
        while(matcher.find()){
            int startIndex = matcher.regionStart();
            if(startIndex>info.index || info.repeatType!=expectRepeatType){
                info.repeatType = expectRepeatType;
                info.index = matcher.regionStart();
                info.patternText = matcher.group();
            }
        }
    }


    static class RepeatInfo{

        String text;
        int repeatType = TextPattern.REPEAT_NONE;
        String patternText;
        int index = -1;

        public RepeatInfo(String text){
            this.text = text;
        }
    }
}
